<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['basic']=$this->plugin_setting('basic');
$adminuid=$return['basic']['adminuid'];
$return['isadmin']=$adminuid==$_SESSION['uid']?1:0;
if($_RQ['op']=='main'){
	set_html('title','会员中心');
    $member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','credit1','credit2','mobile','groupid','avatar','nickname'));
	if(!$member){
		exi('找不到会员信息！','error');
	}
	$this->model('model')->checklogin();	
	$user=pdo_get('jy_quntui_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//VIP时间：
	$user['vipstatus']=$user['viptime']>time()?1:2;
	$user['viptime']=date('Y/m/d',$user['viptime']);
	//是否展示审核入口
	$kefu=pdo_get('jy_quntui_kefus',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'isshow'=>'1'));
	if($kefu){
		$show_sh=1;
	}else{
		$show_sh=0;
	}	
	//总共收到（包含待提现）：
	$return['allred'] = pdo_getsum('jy_quntui_redorder',array('omid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1),'fee');
	$return['shenhenums']=pdo_count('jy_quntui_qun',array('uniacid'=>$_SESSION['uniacid'],'status'=>0));
	$return['member']=$member;
	$return['user']=$user;
	$return['show_sh']=$show_sh;
	$return['setpay']=uni_setting('pay');
	$return['item']=array();
	if($this->plugin_exists('jy_poster')){
		$return['isposter']=true;
	}
	exi($return);
}
//修改用户信息
if($_RQ['op']=='set'){
	set_html('title','用户信息');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
			'nickname'=>$postdata['nickname'],
			'avatar'=>$postdata['avatar'],
			'description'=>$postdata['description'],
			'ewm'=>$postdata['ewm'],
			'province'=>$postdata['pcd']['province'],
			'city'=>$postdata['pcd']['city'],
			'district'=>$postdata['pcd']['district'],
		);
		$user_id=pdo_getcolumn('jy_quntui_user',array('mid'=>$_SESSION['uid']),'id');		
		if($user_id){
			pdo_update('jy_quntui_user',$data,array('id'=>$_SESSION['uid']));
			exi('修改成功','','jy_quntui/home/');		 
		}
	}
	$user=pdo_get('jy_quntui_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$user['pcd']=array(
		'province'=>$user['province'],
		'city'=>$user['city'],
		'district'=>$user['district'],
	);
	$return['item']=$user;
	$return['member']=$member;
	exi($return);
}
//购买会员
if($_RQ['op']=='buyvip'){
	set_html('title','购买会员');
	$basic=$this->plugin_setting('basic');
	$post=array();
	$post['paytype']=1;
	$post['num']=1;
	$post['checked']=true;
	$return['systemxieyi']=$this->plugin_setting('systemxieyi');
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$packnum=$postdata['num'];
		$packname=$postdata['paytype'];
		if($packname==1){
			$fee=$basic['mfee']*$packnum;
		}elseif($packname==2){
			$fee=$basic['jfee']*$packnum;	
		}elseif($packname==3){
			$fee=$basic['yfee']*$packnum;
		}
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'packnum'=>$packnum,
			'ordersn'=>get_ordersn('jy_quntui_viporder','ordersn'),
			'fee'=>$fee,
			'packname'=>$packname,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_quntui_viporder',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']='购买会员订单';
		$params['id']=$id;
		$params['plugin']='jy_quntui';
		$params['pay_type']='wechat';//可选，此项作用为指定支付方式
		$params['no_type']='credit';//可选，此项作用为指定不允许使用哪种支付方式
		$params['back_url']='jy_quntui/home/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
	}
	$return['post']=$post;
	$return['fee']=price($basic['mfee']);
	$return['fee0']=$basic['mfee0']?price($basic['mfee0']):price($basic['mfee']*1.5);
	$basic=$this->plugin_setting('basic');
	$return['vipbg']=$basic['vipbg'];
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_quntui_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_quntui_viporder',array('status'=>1,'ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$user=pdo_get('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if($order['packname']==3){
		$order['packnum']=$order['packnum']*12;
	}elseif($order['packname']==2){
		$order['packnum']=$order['packnum']*3;
	}
	if($user['viptime']!=0){
		
		$addTime=$user['viptime']+$order['packnum']*30*24*3600;
	}else{
		$addTime =$startTime+$order['packnum']*30*24*3600;
	}
	pdo_update('jy_quntui_user',array('isvip'=>1,'viptime'=>$addTime),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	//发送模板消息
	$this->notice('buyvip',$_SESSION['uid'],'',$order['id']);
	exi('支付成功','success','jy_quntui/home/main/');
}

//我发出的红包
if($_RQ['op']=='fred'){
	set_html('title','我发出的红包');
	$list=pdo_getall('jy_quntui_redorder',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','','ispay desc, createtime desc');
	if($list){
		foreach($list as &$item){
			//收红包人信息
			$suser=pdo_get('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['omid']));
			$item['suser']=$suser;
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			//作品信息
			$opus=pdo_get('jy_quntui_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['oid']));
			$item['opus']=$opus;
			if($item['ispay']==0){
				$item['ispay']='待支付';
			}elseif($item['ispay']==1){
					$item['ispay']='已支付';
			}else{
					$item['ispay']='支付失败';
			}
		}
	}
	$return['list']=$list;
	exi($return);
}

//我收到的红包
if($_RQ['op']=='sred'){
	set_html('title','我收到的红包');
	$list=pdo_getall('jy_quntui_redorder',array('uniacid'=>$_SESSION['uniacid'],'omid'=>$_SESSION['uid']),'','','ispay desc, createtime desc');
	if($list){
		foreach($list as &$item){
			//发红包人信息
			$fuser=pdo_get('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$item['fuser']=$fuser;
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			//作品信息
			$opus=pdo_get('jy_quntui_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['oid']));
			$item['opus']=$opus;
			if($item['ispay']==0){
				$item['ispay']='待支付';
			}elseif($item['ispay']==1){
					$item['ispay']='已支付';
			}else{
					$item['ispay']='支付失败';
			}
		}
	}
	$return['list']=$list;
	exi($return);
}
//我的提现记录
if($_RQ['op']=='cashrecord'){
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}
	$list=pdo_getall('jy_quntui_cashrecord',$params,'','createtime desc');
	if($list){
		foreach($list as &$item){
			if($item['status']==0){
				$item['status']='待审核';
			}elseif($item['status']==1){
				$item['status']='提现成功';
			}else{
				$item['status']='提现失败';
			}
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['overtime']=$item['overtime']?date('Y-m-d H:i:s',$item['overtime']):'未完成';
		}
	}
	$return['list']=$list;
	set_html('title','我的提现记录');
	exi($return);
}
//VIP购买记录
if($_RQ['op']=='viplog'){
	set_html('title','VIP购买记录');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($adminuid!=$_SESSION['uid']){
		$params['mid']=$_SESSION['uid'];
	}
	$list=pdo_getall('jy_quntui_viporder',$params,'','createtime desc');
	if($list){
		foreach($list as &$item){
			if($item['ispay']==0){
				$item['ispay']='待支付';
			}else{
				$item['ispay']='支付成功';
			}
			$item['nickname']=pdo_getcolumn('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['packtime']=$item['packtime']?date('Y-m-d H:i:s',$item['packtime']):'无效';
		}
	}
	$return['list']=$list;
	exi($return);
}
//申请提现
if($_RQ['op']=='tixian'){
	set_html('title','提现申请');
	$post=array();
	$post['money']=0;
	$user=pdo_get('jy_quntui_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($_RQ['postdata']){
		   $postdata=get_postdata();
		   $money=$postdata['money'];
		   //被拉黑用户无法提现
		   if($user['status']==1){
			   exi('您已进入黑名单,无法提现，请联系管理员','error');
		   }
		   if(!$money||$money<=0){
			   exi('提现金额不规范，请重新填写提现金额','error');
		   }
		   if($money>$user['cash']){
			   exi('提现金额不得大于可提现金额，请重新填写提现金额','error');
		   }
		   //创建一个状态为正在申请的提现记录
		   $insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'money'=>$money,
				'status'=>0,
				'ordersn'=>get_ordersn('jy_quntui_cashrecord','ordersn'),
				'createtime'=>TIMESTAMP
		   );
		   pdo_insert('jy_quntui_cashrecord',$insert);
		   //修改用户可提现金额
		   pdo_update('jy_quntui_user',array('cash'=>$user['cash']-$money),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		   exi('申请提现成功！','success','jy_quntui/home/cashrecord/');
		
	}
     $return['post']=$post;
     $return['user']=$user;
	 exi($return);
}
